/*
 * igo: com.cctv.igo.admin.scheduler.ui.TaskAction.java Create On 2011-5-3 下午11:21:13 By Q-Wang
 * $Revision: 1.2 $
 */
package com.ctvit.scheduler.ui;

import java.util.List;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.utils.Key;

import com.ctvit.scheduler.domain.QrtxTask;
import com.ctvit.scheduler.domain.QrtxTrigger;
import com.ctvit.scheduler.dto.Task;
import com.ctvit.scheduler.service.TaskService;
import com.ctvit.framework.dao.Pager;
import com.ctvit.framework.struts2.ActionSupport;
import com.ctvit.framework.web.context.ContextHolder;

/**
 * @author <a href="mailto:apeidou@gmail.com">Q-Wang</a>
 *
 */
public class TaskAction extends ActionSupport {

	private TaskService taskService = ContextHolder.getBean(TaskService.class);

	private Pager<QrtxTask> pager;

	private Task task;

	private List<QrtxTask> items;

	private boolean cascade;

	/**
	 * @return the pager
	 */
	public Pager<QrtxTask> getPager() {
		return pager;
	}

	/**
	 * @param pager the pager to set
	 */
	public void setPager(Pager<QrtxTask> pager) {
		this.pager = pager;
	}

	/**
	 * @return the task
	 */
	public Task getTask() {
		return task;
	}

	/**
	 * @param task the task to set
	 */
	public void setTask(Task task) {
		this.task = task;
	}

	/**
	 * @return the items
	 */
	public List<QrtxTask> getItems() {
		return items;
	}

	/**
	 * @param items the items to set
	 */
	public void setItems(List<QrtxTask> items) {
		this.items = items;
	}

	/**
	 * @param cascade the cascade to set
	 */
	public void setCascade(boolean cascade) {
		this.cascade = cascade;
	}



	public String add() {
		task = null;
		return "add";
	}

	public String edit() {
		task = taskService.get(task.getId());
		return "edit";
	}

	public String update() {
		taskService.update(task);
		addActionMessage("计划任务 任务'"+task.getName()+"'更新成功.");
		task = new Task();
		return list();
	}

	public String save() {
		taskService.save(task);
		addActionMessage("计划任务 任务'"+task.getName()+"'保存成功.");
		task = new Task();
		return list();
	}
	public String  startTask() {
		Task taskStart = null;
		try {
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
			scheduler.start();
			taskStart = taskService.get(task.getId());
			JobDetail detailJob = taskStart.createJob();
			scheduler.addJob(detailJob,true);
			Key key=detailJob.getKey();
			scheduler.triggerJob(key.getName(),key.getGroup());
			this.addActionMessage(taskStart.getName()+"成功执行完成！");
		} catch (Exception e) {
			log.debug(e);
			this.addActionMessage(taskStart.getName()+"执行出错，请通知系统管理员！");
		}
		return list();
	}
	public String list() {
		if (pager==null) {
			pager = new Pager<QrtxTask>();
		}
		if(task==null){
			task=new Task();
		}
		pager.setQuery(task);
		taskService.list(pager);
		items = pager.getResult();
		
		
//		if (pager==null) {
//			pager = new Pager<QrtxTrigger>();
//		}
//		pager.setQuery(trigger);
//		triggerService.list(pager);
//		items = pager.getResult();
		return "list";
	}

	public String delete() {
		taskService.delete(task.getId(), cascade);
		addActionMessage("计划任务 任务'id="+task.getId()+"'删除成功.");
		task = null;
		return list();
	}


	public String browse() {
		if (pager==null) {
			pager = new Pager<QrtxTask>();
		}
		pager.setQuery(task);
		taskService.list(pager);
		items = pager.getResult();
		return "browse";
	}
}
